<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>上下文 | 课程简介</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../C05-Flask-RESTful/index.html" />
    
    
    <link rel="prev" href="../C04-HookAndContext/Hook.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="5.3"
        data-chapter-title="上下文"
        data-filepath="C04-HookAndContext/Context.md"
        data-basepath=".."
        data-revision="Sat May 11 2019 20:06:35 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        课程简介
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="AboutFlask.html">
            
                
                    <a href="../AboutFlask.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        Flask介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="C01-CreateProject/index.html">
            
                
                    <a href="../C01-CreateProject/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        工程搭建
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="C01-CreateProject/EnvInstall.html">
            
                
                    <a href="../C01-CreateProject/EnvInstall.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        环境安装
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="C01-CreateProject/HelloWorld.html">
            
                
                    <a href="../C01-CreateProject/HelloWorld.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        HelloWorld程序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="C01-CreateProject/AboutParams.html">
            
                
                    <a href="../C01-CreateProject/AboutParams.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        参数说明
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="C01-CreateProject/StartDevelopmentServer.html">
            
                
                    <a href="../C01-CreateProject/StartDevelopmentServer.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        开发服务器运行方式
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="C02-RouterAndBlueprint/index.html">
            
                
                    <a href="../C02-RouterAndBlueprint/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        路由与蓝图
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="C02-RouterAndBlueprint/Router.html">
            
                
                    <a href="../C02-RouterAndBlueprint/Router.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        路由
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="C02-RouterAndBlueprint/Blueprint.html">
            
                
                    <a href="../C02-RouterAndBlueprint/Blueprint.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        蓝图
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="C03-RequestAndResponse/index.html">
            
                
                    <a href="../C03-RequestAndResponse/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        请求与响应
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="C03-RequestAndResponse/HandleRequest.html">
            
                
                    <a href="../C03-RequestAndResponse/HandleRequest.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        处理请求
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="C03-RequestAndResponse/HandleResponse.html">
            
                
                    <a href="../C03-RequestAndResponse/HandleResponse.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        处理响应
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="C03-RequestAndResponse/CookieAndSession.html">
            
                
                    <a href="../C03-RequestAndResponse/CookieAndSession.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        Cookie与Session
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="C04-HookAndContext/index.html">
            
                
                    <a href="../C04-HookAndContext/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        请求钩子与上下文
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="C04-HookAndContext/AbortAndErrorhandler.html">
            
                
                    <a href="../C04-HookAndContext/AbortAndErrorhandler.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        异常处理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="C04-HookAndContext/Hook.html">
            
                
                    <a href="../C04-HookAndContext/Hook.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        请求钩子
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="5.3" data-path="C04-HookAndContext/Context.html">
            
                
                    <a href="../C04-HookAndContext/Context.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        上下文
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="C05-Flask-RESTful/index.html">
            
                
                    <a href="../C05-Flask-RESTful/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        Flask-RESTful
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="C05-Flask-RESTful/QuickStart.html">
            
                
                    <a href="../C05-Flask-RESTful/QuickStart.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        起步
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="C05-Flask-RESTful/AboutView.html">
            
                
                    <a href="../C05-Flask-RESTful/AboutView.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        关于视图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="C05-Flask-RESTful/AboutRequest.html">
            
                
                    <a href="../C05-Flask-RESTful/AboutRequest.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        关于请求
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="C05-Flask-RESTful/AboutResponse.html">
            
                
                    <a href="../C05-Flask-RESTful/AboutResponse.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        关于响应
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >课程简介</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x4E0A;&#x4E0B;&#x6587;">&#x4E0A;&#x4E0B;&#x6587;</h1>
<p>&#x4E0A;&#x4E0B;&#x6587;&#xFF1A;&#x5373;&#x8BED;&#x5883;&#xFF0C;&#x8BED;&#x610F;&#xFF0C;&#x5728;&#x7A0B;&#x5E8F;&#x4E2D;&#x53EF;&#x4EE5;&#x7406;&#x89E3;&#x4E3A;&#x5728;&#x4EE3;&#x7801;&#x6267;&#x884C;&#x5230;&#x67D0;&#x4E00;&#x65F6;&#x523B;&#x65F6;&#xFF0C;&#x6839;&#x636E;&#x4E4B;&#x524D;&#x4EE3;&#x7801;&#x6240;&#x505A;&#x7684;&#x64CD;&#x4F5C;&#x4EE5;&#x53CA;&#x4E0B;&#x6587;&#x5373;&#x5C06;&#x8981;&#x6267;&#x884C;&#x7684;&#x903B;&#x8F91;&#xFF0C;&#x53EF;&#x4EE5;&#x51B3;&#x5B9A;&#x5728;&#x5F53;&#x524D;&#x65F6;&#x523B;&#x4E0B;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5230;&#x7684;&#x53D8;&#x91CF;&#xFF0C;&#x6216;&#x8005;&#x53EF;&#x4EE5;&#x5B8C;&#x6210;&#x7684;&#x4E8B;&#x60C5;&#x3002;</p>
<p>Flask&#x4E2D;&#x6709;&#x4E24;&#x79CD;&#x4E0A;&#x4E0B;&#x6587;&#xFF0C;&#x8BF7;&#x6C42;&#x4E0A;&#x4E0B;&#x6587;&#x548C;&#x5E94;&#x7528;&#x4E0A;&#x4E0B;&#x6587;</p>
<p>Flask&#x4E2D;&#x4E0A;&#x4E0B;&#x6587;&#x5BF9;&#x8C61;&#xFF1A;&#x76F8;&#x5F53;&#x4E8E;&#x4E00;&#x4E2A;&#x5BB9;&#x5668;&#xFF0C;&#x4FDD;&#x5B58;&#x4E86; Flask &#x7A0B;&#x5E8F;&#x8FD0;&#x884C;&#x8FC7;&#x7A0B;&#x4E2D;&#x7684;&#x4E00;&#x4E9B;&#x4FE1;&#x606F;&#x3002;</p>
<h2 id="1-&#x8BF7;&#x6C42;&#x4E0A;&#x4E0B;&#x6587;request-context">1 &#x8BF7;&#x6C42;&#x4E0A;&#x4E0B;&#x6587;(request context)</h2>
<p>&#x601D;&#x8003;&#xFF1A;&#x5728;&#x89C6;&#x56FE;&#x51FD;&#x6570;&#x4E2D;&#xFF0C;&#x5982;&#x4F55;&#x53D6;&#x5230;&#x5F53;&#x524D;&#x8BF7;&#x6C42;&#x7684;&#x76F8;&#x5173;&#x6570;&#x636E;&#xFF1F;&#x6BD4;&#x5982;&#xFF1A;&#x8BF7;&#x6C42;&#x5730;&#x5740;&#xFF0C;&#x8BF7;&#x6C42;&#x65B9;&#x5F0F;&#xFF0C;cookie&#x7B49;&#x7B49;</p>
<p>&#x5728; flask &#x4E2D;&#xFF0C;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x5728;&#x89C6;&#x56FE;&#x51FD;&#x6570;&#x4E2D;&#x4F7F;&#x7528; <strong>request</strong> &#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x8FDB;&#x884C;&#x83B7;&#x53D6;&#x76F8;&#x5173;&#x6570;&#x636E;&#xFF0C;&#x800C; <strong>request</strong> &#x5C31;&#x662F;&#x8BF7;&#x6C42;&#x4E0A;&#x4E0B;&#x6587;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x4FDD;&#x5B58;&#x4E86;&#x5F53;&#x524D;&#x672C;&#x6B21;&#x8BF7;&#x6C42;&#x7684;&#x76F8;&#x5173;&#x6570;&#x636E;&#xFF0C;&#x8BF7;&#x6C42;&#x4E0A;&#x4E0B;&#x6587;&#x5BF9;&#x8C61;&#x6709;&#xFF1A;request&#x3001;session</p>
<ul>
<li>request <ul>
<li>&#x5C01;&#x88C5;&#x4E86;HTTP&#x8BF7;&#x6C42;&#x7684;&#x5185;&#x5BB9;&#xFF0C;&#x9488;&#x5BF9;&#x7684;&#x662F;http&#x8BF7;&#x6C42;&#x3002;&#x4E3E;&#x4F8B;&#xFF1A;user = request.args.get(&apos;user&apos;)&#xFF0C;&#x83B7;&#x53D6;&#x7684;&#x662F;get&#x8BF7;&#x6C42;&#x7684;&#x53C2;&#x6570;&#x3002;</li>
</ul>
</li>
<li>session<ul>
<li>&#x7528;&#x6765;&#x8BB0;&#x5F55;&#x8BF7;&#x6C42;&#x4F1A;&#x8BDD;&#x4E2D;&#x7684;&#x4FE1;&#x606F;&#xFF0C;&#x9488;&#x5BF9;&#x7684;&#x662F;&#x7528;&#x6237;&#x4FE1;&#x606F;&#x3002;&#x4E3E;&#x4F8B;&#xFF1A;session[&apos;name&apos;] = user.id&#xFF0C;&#x53EF;&#x4EE5;&#x8BB0;&#x5F55;&#x7528;&#x6237;&#x4FE1;&#x606F;&#x3002;&#x8FD8;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;session.get(&apos;name&apos;)&#x83B7;&#x53D6;&#x7528;&#x6237;&#x4FE1;&#x606F;&#x3002;</li>
</ul>
</li>
</ul>
<h2 id="2-&#x5E94;&#x7528;&#x4E0A;&#x4E0B;&#x6587;application-context">2 &#x5E94;&#x7528;&#x4E0A;&#x4E0B;&#x6587;(application context)</h2>
<p>&#x5B83;&#x7684;&#x5B57;&#x9762;&#x610F;&#x601D;&#x662F; &#x5E94;&#x7528;&#x4E0A;&#x4E0B;&#x6587;&#xFF0C;&#x4F46;&#x5B83;&#x4E0D;&#x662F;&#x4E00;&#x76F4;&#x5B58;&#x5728;&#x7684;&#xFF0C;&#x5B83;&#x53EA;&#x662F;request context &#x4E2D;&#x7684;&#x4E00;&#x4E2A;&#x5BF9; app &#x7684;&#x4EE3;&#x7406;(&#x4EBA;)&#xFF0C;&#x6240;&#x8C13;local proxy&#x3002;&#x5B83;&#x7684;&#x4F5C;&#x7528;&#x4E3B;&#x8981;&#x662F;&#x5E2E;&#x52A9; request &#x83B7;&#x53D6;&#x5F53;&#x524D;&#x7684;&#x5E94;&#x7528;&#xFF0C;&#x5B83;&#x662F;&#x4F34; request &#x800C;&#x751F;&#xFF0C;&#x968F; request &#x800C;&#x706D;&#x7684;&#x3002;</p>
<p>&#x5E94;&#x7528;&#x4E0A;&#x4E0B;&#x6587;&#x5BF9;&#x8C61;&#x6709;&#xFF1A;current_app&#xFF0C;g</p>
<h3 id="currentapp">current_app</h3>
<p>&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x4E0A;&#x4E0B;&#x6587;,&#x7528;&#x4E8E;&#x5B58;&#x50A8;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x4E2D;&#x7684;&#x53D8;&#x91CF;&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;current_app.name&#x6253;&#x5370;&#x5F53;&#x524D;app&#x7684;&#x540D;&#x79F0;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x5728;current_app&#x4E2D;&#x5B58;&#x50A8;&#x4E00;&#x4E9B;&#x53D8;&#x91CF;&#xFF0C;&#x4F8B;&#x5982;&#xFF1A;</p>
<ul>
<li>&#x5E94;&#x7528;&#x7684;&#x542F;&#x52A8;&#x811A;&#x672C;&#x662F;&#x54EA;&#x4E2A;&#x6587;&#x4EF6;&#xFF0C;&#x542F;&#x52A8;&#x65F6;&#x6307;&#x5B9A;&#x4E86;&#x54EA;&#x4E9B;&#x53C2;&#x6570;</li>
<li>&#x52A0;&#x8F7D;&#x4E86;&#x54EA;&#x4E9B;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#xFF0C;&#x5BFC;&#x5165;&#x4E86;&#x54EA;&#x4E9B;&#x914D;&#x7F6E;</li>
<li>&#x8FDE;&#x4E86;&#x54EA;&#x4E2A;&#x6570;&#x636E;&#x5E93;</li>
<li>&#x6709;&#x54EA;&#x4E9B;public&#x7684;&#x5DE5;&#x5177;&#x7C7B;&#x3001;&#x5E38;&#x91CF;</li>
<li>&#x5E94;&#x7528;&#x8DD1;&#x518D;&#x54EA;&#x4E2A;&#x673A;&#x5668;&#x4E0A;&#xFF0C;IP&#x591A;&#x5C11;&#xFF0C;&#x5185;&#x5B58;&#x591A;&#x5927;</li>
</ul>
<h4 id="&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</h4>
<p>&#x521B;&#x5EFA;<code>current_app_demo.py</code></p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> Flask, current_app

app1 = Flask(__name__)
app2 = Flask(__name__)

<span class="hljs-comment"># &#x4EE5;redis&#x5BA2;&#x6237;&#x7AEF;&#x5BF9;&#x8C61;&#x4E3A;&#x4F8B;</span>
<span class="hljs-comment"># &#x7528;&#x5B57;&#x7B26;&#x4E32;&#x8868;&#x793A;&#x521B;&#x5EFA;&#x7684;redis&#x5BA2;&#x6237;&#x7AEF;</span>
<span class="hljs-comment"># &#x4E3A;&#x4E86;&#x65B9;&#x4FBF;&#x5728;&#x5404;&#x4E2A;&#x89C6;&#x56FE;&#x4E2D;&#x4F7F;&#x7528;&#xFF0C;&#x5C06;&#x521B;&#x5EFA;&#x7684;redis&#x5BA2;&#x6237;&#x7AEF;&#x5BF9;&#x8C61;&#x4FDD;&#x5B58;&#x5230;flask app&#x4E2D;&#xFF0C;</span>
<span class="hljs-comment"># &#x540E;&#x7EED;&#x53EF;&#x4EE5;&#x5728;&#x89C6;&#x56FE;&#x4E2D;&#x4F7F;&#x7528;current_app.redis_cli&#x83B7;&#x53D6;</span>
app1.redis_cli = <span class="hljs-string">&apos;app1 redis client&apos;</span>
app2.redis_cli = <span class="hljs-string">&apos;app2 redis client&apos;</span>

<span class="hljs-decorator">@app1.route(&apos;/route11&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">route11</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">return</span> current_app.redis_cli

<span class="hljs-decorator">@app1.route(&apos;/route12&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">route12</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">return</span> current_app.redis_cli

<span class="hljs-decorator">@app2.route(&apos;/route21&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">route21</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">return</span> current_app.redis_cli

<span class="hljs-decorator">@app2.route(&apos;/route22&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">route22</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">return</span> current_app.redis_cli
</code></pre>
<p>&#x8FD0;&#x884C;</p>
<pre><code class="lang-shell">export FLASK_APP=current_app_demo:app1
flask run
</code></pre>
<ul>
<li>&#x8BBF;&#x95EE;<code>/route11</code>  &#x663E;&#x793A;<code>app1 redis client</code></li>
<li>&#x8BBF;&#x95EE;<code>/route12</code>  &#x663E;&#x793A;<code>app1 redis client</code></li>
</ul>
<pre><code class="lang-shell">export FLASK_APP=current_app_demo:app2
flask run
</code></pre>
<ul>
<li>&#x8BBF;&#x95EE;<code>/route21</code>  &#x663E;&#x793A;<code>app2 redis client</code></li>
<li>&#x8BBF;&#x95EE;<code>/route22</code>  &#x663E;&#x793A;<code>app2 redis client</code></li>
</ul>
<h4 id="&#x4F5C;&#x7528;">&#x4F5C;&#x7528;</h4>
<p><strong><code>current_app</code> &#x5C31;&#x662F;&#x5F53;&#x524D;&#x8FD0;&#x884C;&#x7684;flask app&#xFF0C;&#x5728;&#x4EE3;&#x7801;&#x4E0D;&#x65B9;&#x4FBF;&#x76F4;&#x63A5;&#x64CD;&#x4F5C;flask&#x7684;app&#x5BF9;&#x8C61;&#x65F6;&#xFF0C;&#x53EF;&#x4EE5;&#x64CD;&#x4F5C;<code>current_app</code>&#x5C31;&#x7B49;&#x4EF7;&#x4E8E;&#x64CD;&#x4F5C;flask app&#x5BF9;&#x8C61;</strong></p>
<h3 id="g&#x5BF9;&#x8C61;">g&#x5BF9;&#x8C61;</h3>
<p>g &#x4F5C;&#x4E3A; flask &#x7A0B;&#x5E8F;&#x5168;&#x5C40;&#x7684;&#x4E00;&#x4E2A;&#x4E34;&#x65F6;&#x53D8;&#x91CF;&#xFF0C;&#x5145;&#x5F53;&#x4E2D;&#x95F4;&#x5A92;&#x4ECB;&#x7684;&#x4F5C;&#x7528;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x5B83;&#x5728;&#x4E00;&#x6B21;&#x8BF7;&#x6C42;&#x8C03;&#x7528;&#x7684;&#x591A;&#x4E2A;&#x51FD;&#x6570;&#x95F4;&#x4F20;&#x9012;&#x4E00;&#x4E9B;&#x6570;&#x636E;&#x3002;&#x6BCF;&#x6B21;&#x8BF7;&#x6C42;&#x90FD;&#x4F1A;&#x91CD;&#x8BBE;&#x8FD9;&#x4E2A;&#x53D8;&#x91CF;&#x3002;</p>
<h4 id="&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</h4>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> Flask, g

app = Flask(__name__)

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">db_query</span><span class="hljs-params">()</span>:</span>
    user_id = g.user_id
    user_name = g.user_name
    print(<span class="hljs-string">&apos;user_id={} user_name={}&apos;</span>.format(user_id, user_name))

<span class="hljs-decorator">@app.route(&apos;/&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_user_profile</span><span class="hljs-params">()</span>:</span>
    g.user_id = <span class="hljs-number">123</span>
    g.user_name = <span class="hljs-string">&apos;itcast&apos;</span>
    db_query()
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;hello world&apos;</span>
</code></pre>
<h3 id="g&#x5BF9;&#x8C61;&#x4E0E;&#x8BF7;&#x6C42;&#x94A9;&#x5B50;&#x7684;&#x7EFC;&#x5408;&#x6848;&#x4F8B;">g&#x5BF9;&#x8C61;&#x4E0E;&#x8BF7;&#x6C42;&#x94A9;&#x5B50;&#x7684;&#x7EFC;&#x5408;&#x6848;&#x4F8B;</h3>
<h4 id="&#x9700;&#x6C42;">&#x9700;&#x6C42;</h4>
<ul>
<li>&#x6784;&#x5EFA;&#x8BA4;&#x8BC1;&#x673A;&#x5236;</li>
<li>&#x5BF9;&#x4E8E;&#x7279;&#x5B9A;&#x89C6;&#x56FE;&#x53EF;&#x4EE5;&#x63D0;&#x4F9B;&#x5F3A;&#x5236;&#x8981;&#x6C42;&#x7528;&#x6237;&#x767B;&#x5F55;&#x7684;&#x9650;&#x5236;</li>
<li>&#x5BF9;&#x4E8E;&#x6240;&#x6709;&#x89C6;&#x56FE;&#xFF0C;&#x65E0;&#x8BBA;&#x662F;&#x5426;&#x5F3A;&#x5236;&#x8981;&#x6C42;&#x7528;&#x6237;&#x767B;&#x5F55;&#xFF0C;&#x90FD;&#x53EF;&#x4EE5;&#x5728;&#x89C6;&#x56FE;&#x4E2D;&#x5C1D;&#x8BD5;&#x83B7;&#x53D6;&#x7528;&#x6237;&#x8BA4;&#x8BC1;&#x540E;&#x7684;&#x8EAB;&#x4EFD;&#x4FE1;&#x606F;</li>
</ul>
<h4 id="&#x5B9E;&#x73B0;">&#x5B9E;&#x73B0;</h4>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> Flask, abort, g

app = Flask(__name__)

<span class="hljs-decorator">@app.before_request</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">authentication</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;
    &#x5229;&#x7528;before_request&#x8BF7;&#x6C42;&#x94A9;&#x5B50;&#xFF0C;&#x5728;&#x8FDB;&#x5165;&#x6240;&#x6709;&#x89C6;&#x56FE;&#x524D;&#x5148;&#x5C1D;&#x8BD5;&#x5224;&#x65AD;&#x7528;&#x6237;&#x8EAB;&#x4EFD;
    :return:
    &quot;&quot;&quot;</span>
    <span class="hljs-comment"># TODO &#x6B64;&#x5904;&#x5229;&#x7528;&#x9274;&#x6743;&#x673A;&#x5236;&#xFF08;&#x5982;cookie&#x3001;session&#x3001;jwt&#x7B49;&#xFF09;&#x9274;&#x522B;&#x7528;&#x6237;&#x8EAB;&#x4EFD;&#x4FE1;&#x606F;</span>
    <span class="hljs-comment"># if &#x5DF2;&#x767B;&#x5F55;&#x7528;&#x6237;&#xFF0C;&#x7528;&#x6237;&#x6709;&#x8EAB;&#x4EFD;&#x4FE1;&#x606F;</span>
    g.user_id = <span class="hljs-number">123</span>
    <span class="hljs-comment"># else &#x672A;&#x767B;&#x5F55;&#x7528;&#x6237;&#xFF0C;&#x7528;&#x6237;&#x65E0;&#x8EAB;&#x4EFD;&#x4FE1;&#x606F;</span>
    <span class="hljs-comment"># g.user_id = None</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">login_required</span><span class="hljs-params">(func)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">wrapper</span><span class="hljs-params">(*args, **kwargs)</span>:</span>
        <span class="hljs-keyword">if</span> g.user_id <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">None</span>:
            <span class="hljs-keyword">return</span> func(*args, **kwargs)
        <span class="hljs-keyword">else</span>:
            abort(<span class="hljs-number">401</span>)

    <span class="hljs-keyword">return</span> wrapper

<span class="hljs-decorator">@app.route(&apos;/&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">index</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;home page user_id={}&apos;</span>.format(g.user_id)

<span class="hljs-decorator">@app.route(&apos;/profile&apos;)</span>
<span class="hljs-decorator">@login_required</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_user_profile</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;user profile page user_id={}&apos;</span>.format(g.user_id)
</code></pre>
<h2 id="3-appcontext-&#x4E0E;-requestcontext">3 app_context &#x4E0E; request_context</h2>
<h4 id="&#x601D;&#x8003;">&#x601D;&#x8003;</h4>
<p>&#x5728;Flask&#x7A0B;&#x5E8F;&#x672A;&#x8FD0;&#x884C;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x8C03;&#x8BD5;&#x4EE3;&#x7801;&#x65F6;&#x9700;&#x8981;&#x4F7F;&#x7528;<code>current_app</code>&#x3001;<code>g</code>&#x3001;<code>request</code>&#x8FD9;&#x4E9B;&#x5BF9;&#x8C61;&#xFF0C;&#x4F1A;&#x4E0D;&#x4F1A;&#x6709;&#x95EE;&#x9898;&#xFF1F;&#x8BE5;&#x5982;&#x4F55;&#x4F7F;&#x7528;&#xFF1F;</p>
<h4 id="appcontext">app_context</h4>
<p><code>app_context</code>&#x4E3A;&#x6211;&#x4EEC;&#x63D0;&#x4F9B;&#x4E86;&#x5E94;&#x7528;&#x4E0A;&#x4E0B;&#x6587;&#x73AF;&#x5883;&#xFF0C;&#x5141;&#x8BB8;&#x6211;&#x4EEC;&#x5728;&#x5916;&#x90E8;&#x4F7F;&#x7528;&#x5E94;&#x7528;&#x4E0A;&#x4E0B;&#x6587;<code>current_app</code>&#x3001;<code>g</code></p>
<p>&#x53EF;&#x4EE5;&#x901A;&#x8FC7;<code>with</code>&#x8BED;&#x53E5;&#x8FDB;&#x884C;&#x4F7F;&#x7528;</p>
<pre><code class="lang-python"><span class="hljs-prompt">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> Flask
<span class="hljs-prompt">&gt;&gt;&gt; </span>app = Flask(<span class="hljs-string">&apos;&apos;</span>)
<span class="hljs-prompt">&gt;&gt;&gt; </span>app.redis_cli = <span class="hljs-string">&apos;redis client&apos;</span>
<span class="hljs-prompt">&gt;&gt;&gt; </span>
<span class="hljs-prompt">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> current_app
<span class="hljs-prompt">&gt;&gt;&gt; </span>current_app.redis_cli   <span class="hljs-comment"># &#x9519;&#x8BEF;&#xFF0C;&#x6CA1;&#x6709;&#x4E0A;&#x4E0B;&#x6587;&#x73AF;&#x5883;</span>
&#x62A5;&#x9519;
<span class="hljs-prompt">&gt;&gt;&gt; </span><span class="hljs-keyword">with</span> app.app_context():  <span class="hljs-comment"># &#x501F;&#x52A9;with&#x8BED;&#x53E5;&#x4F7F;&#x7528;app_context&#x521B;&#x5EFA;&#x5E94;&#x7528;&#x4E0A;&#x4E0B;&#x6587;</span>
<span class="hljs-prompt">... </span>    print(current_app.redis_cli)
...
redis client
</code></pre>
<h4 id="requestcontext">request_context</h4>
<p><code>request_context</code>&#x4E3A;&#x6211;&#x4EEC;&#x63D0;&#x4F9B;&#x4E86;&#x8BF7;&#x6C42;&#x4E0A;&#x4E0B;&#x6587;&#x73AF;&#x5883;&#xFF0C;&#x5141;&#x8BB8;&#x6211;&#x4EEC;&#x5728;&#x5916;&#x90E8;&#x4F7F;&#x7528;&#x8BF7;&#x6C42;&#x4E0A;&#x4E0B;&#x6587;<code>request</code>&#x3001;<code>session</code></p>
<p>&#x53EF;&#x4EE5;&#x901A;&#x8FC7;with&#x8BED;&#x53E5;&#x8FDB;&#x884C;&#x4F7F;&#x7528;</p>
<pre><code class="lang-python"><span class="hljs-prompt">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> Flask
<span class="hljs-prompt">&gt;&gt;&gt; </span>app = Flask(<span class="hljs-string">&apos;&apos;</span>)
<span class="hljs-prompt">&gt;&gt;&gt; </span>request.args  <span class="hljs-comment"># &#x9519;&#x8BEF;&#xFF0C;&#x6CA1;&#x6709;&#x4E0A;&#x4E0B;&#x6587;&#x73AF;&#x5883;</span>
&#x62A5;&#x9519;
<span class="hljs-prompt">&gt;&gt;&gt; </span>environ = {<span class="hljs-string">&apos;wsgi.version&apos;</span>:(<span class="hljs-number">1</span>,<span class="hljs-number">0</span>), <span class="hljs-string">&apos;wsgi.input&apos;</span>: <span class="hljs-string">&apos;&apos;</span>, <span class="hljs-string">&apos;REQUEST_METHOD&apos;</span>: <span class="hljs-string">&apos;GET&apos;</span>, <span class="hljs-string">&apos;PATH_INFO&apos;</span>: <span class="hljs-string">&apos;/&apos;</span>, <span class="hljs-string">&apos;SERVER_NAME&apos;</span>: <span class="hljs-string">&apos;itcast server&apos;</span>, <span class="hljs-string">&apos;wsgi.url_scheme&apos;</span>: <span class="hljs-string">&apos;http&apos;</span>, <span class="hljs-string">&apos;SERVER_PORT&apos;</span>: <span class="hljs-string">&apos;80&apos;</span>}  <span class="hljs-comment"># &#x6A21;&#x62DF;&#x89E3;&#x6790;&#x5BA2;&#x6237;&#x7AEF;&#x8BF7;&#x6C42;&#x4E4B;&#x540E;&#x7684;wsgi&#x5B57;&#x5178;&#x6570;&#x636E;</span>
<span class="hljs-prompt">&gt;&gt;&gt; </span><span class="hljs-keyword">with</span> app.request_context(environ):  <span class="hljs-comment"># &#x501F;&#x52A9;with&#x8BED;&#x53E5;&#x4F7F;&#x7528;request_context&#x521B;&#x5EFA;&#x8BF7;&#x6C42;&#x4E0A;&#x4E0B;&#x6587;</span>
<span class="hljs-prompt">... </span>    print(request.path)
<span class="hljs-prompt">... </span>  
/
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../C04-HookAndContext/Hook.html" class="navigation navigation-prev " aria-label="Previous page: 请求钩子"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../C05-Flask-RESTful/index.html" class="navigation navigation-next " aria-label="Next page: Flask-RESTful"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
